﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;

namespace TVGuide.Common
{
    public static class Utils
    {

        /// <summary>
        /// Sending e-mail message to arm.tvguide@gmail.com adress, with the parsing issue described.
        /// </summary>
        /// <param name="message">Body of the e-mail.</param>
        public static void SendNotificationMail(string message)
        {

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("arm.tvguide@gmail.com", "tvguidearm"),
                EnableSsl = true
            };
            //client.Send("arm.tvguide@gmail.com", "arm.tvguide@gmail.com", "Failed action", message);
        }

        /// <summary>
        /// Chopping all HTML tags from the string.
        /// </summary>
        /// <param name="input">Input string</param>
        /// <returns></returns>
        public static string removeHTMLTags(string input)
        {
            Regex rgxTagCutter = new Regex("</?[^>]*>");
            string stringWithoutTags = rgxTagCutter.Replace(input, " ");
            return stringWithoutTags;
        }

        /// <summary>
        /// Extension method for MatchCollection.
        /// </summary>
        /// <param name="inputCollection">Input MatchCollection.</param>
        /// <returns>
        /// Returns HTML tag free string list from MatchCollection.
        /// </returns>
        public static List<string> removeHTMLTags(this MatchCollection inputCollection)
        {
            List<string> outputList = new List<string>();
            foreach (Match inputMatch in inputCollection)
            {

                outputList.Add(removeHTMLTags(inputMatch.Value));
            }


            return outputList;
        }

        /// <summary>
        /// Seperates date and program name from string represented as: 00:00 Some TV Program
        /// </summary>
        /// <param name="input">Input string to extract time and program name.</param>
        /// <param name="time">Time of the program.</param>
        /// <param name="programName">Program name.</param>
        public static void seperateTimeProgramName(string input, out int hour, out int minutes, out string programName)
        {         
            string programDateStringPattern = @"(\d+)[:\.](\d{2})";
            Regex rgxProgramDateString = new Regex(programDateStringPattern);
            Match dateMatch = rgxProgramDateString.Match(removeHTMLTags(input));
            hour = Int32.Parse(dateMatch.Groups[1].Value);
            minutes = Int32.Parse(dateMatch.Groups[2].Value);
            if (hour==24)
            {

                hour = 0;
            }
            
            string programNameStringPattern = @"\d{2}:\d{2}(.*)";
            Regex rgxProgramNameString = new Regex(programNameStringPattern);
            Match programNameMatch = rgxProgramNameString.Match(input);
            programName = programNameMatch.Groups[1].Value;
        }

        public static void seperateTimeProgramName(string input, out int hour, out int minutes)
        {
            string dummy;
            seperateTimeProgramName(input, out hour, out minutes, out dummy);
        }


        public static string EscapeSpacialSymbols(string input)
        {
            string ntv = "NTV+";
            Regex rgxSpacialSymbol = new Regex("([+*?])");
            Match match = rgxSpacialSymbol.Match(input);
            string stringWithoutTags = rgxSpacialSymbol.Replace(input, "\\" + rgxSpacialSymbol.Match(ntv).Groups[1].Value);
            return stringWithoutTags;
        }

        static private int GetNumber(char ch)
        {
            int a = (int)ch;

            if (a >= 178 && a <= 253)
                return 178;

            if (a >= 1377 && a <= 1414)
                return 1377;

            if (a >= 1329 && a <= 1366)
                return 1329;

            return a;
        }
 public static String ArmUnicodeToArmAnsi(string UnicodeString)
        {
            string ansi_str = "";
            foreach (Char c in UnicodeString)
            {
                switch (GetIntervalNumber(c))
                {
                    case 1415: //ev
                        ansi_str += ((Char)168).ToString();
                        break;

                    case 1329: // TODO: to 1366
                        ansi_str += ((Char)((c - 1240) * 2)).ToString();
                        break;

                    case 1377: // TODO: to 1414
                        ansi_str += ((Char)((c - 1288) * 2 + 1)).ToString();
                        break;

                    case 1373: //`
                        ansi_str += ((Char)170).ToString();
                        break;

                    case 171: // <<
                        ansi_str += ((Char)167).ToString();
                        break;

                    case 187: // >>
                        ansi_str += ((Char)166).ToString();
                        break;

                    case 1371: // shesht
                        ansi_str += ((Char)176).ToString();
                        break;

                    case 1374: // ?
                        ansi_str += ((Char)177).ToString();
                        break;

                    default:
                        ansi_str += c;
                        break;
                }
            }

            return ansi_str; ;
        }

        static private int GetIntervalNumber(char ch)
        {
            int a = (int)ch;

            if (a >= 178 && a <= 253)
                return 178;

            if (a >= 1377 && a <= 1414)
                return 1377;

            if (a >= 1329 && a <= 1366)
                return 1329;

            return a;
        }

        static public String ArmToUnicode(string AsciiString)
        {
            string unicde_str = "";
            foreach (Char c in AsciiString)
            {
                switch (GetNumber(c))
                {
                    case 168: //ev
                        unicde_str += ((Char)1415).ToString();
                        break;

                    case 178: // TODO: to 253
                        unicde_str += ((Char)(((c % 2) * 48) + ((c - 178) / 2) + 1329)).ToString();
                        break;

                    case 170: //`
                        unicde_str += ((Char)1373).ToString();
                        break;

                    case 167: // <<
                        unicde_str += ((Char)171).ToString();
                        break;

                    case 166: // >>
                        unicde_str += ((Char)187).ToString();
                        break;

                    case 176: // shesht
                        unicde_str += ((Char)1371).ToString();
                        break;

                    case 177: // ?
                        unicde_str += ((Char)1374).ToString();
                        break;

                    case 171: // ,
                        unicde_str += ((Char)44).ToString();
                        break;

                    default:
                        unicde_str += c;
                        break;
                }
            }

            return unicde_str;
        }


       
    }
}
